/**
 * @author Francesco Rosso, matr 592057
 */

package pcd1112.model;

import java.math.BigDecimal;

/**
 * This class represents a point in the unit square area
 */
public class Coordinata {
	private double x, y;

	public Coordinata(double _x, double _y) {
		this.x = _x;
		this.y = _y;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;

		if ((o == null) || (o.getClass() != this.getClass()))
			return false;

		Coordinata c = (Coordinata) o;
		return (x == c.getX() && y == c.getY());
	}

	/**
	 * Necessary because of equals(Object) overriding
	 */
	@Override
	public int hashCode() {
		int hash = 1;
		hash = hash * 17 + ((int) x * 200);
		hash = hash * 17 + ((int) y * 60);
		return hash;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public void setX(double _x) {
		this.x = _x;
	}

	public void setY(double _y) {
		this.y = _y;
	}

	/**
	 * Returns true if the centro parameter is included in the raggio (radius)
	 * specified
	 * 
	 * @param centro
	 * @param raggio
	 * @return
	 */
	public boolean compresaInArea(Coordinata centro, double raggio) {

		double xDifference = centro.getX() - getX();
		double yDifference = centro.getY() - getY();
		// we compute the distance between two points using the pitagora theorem
		double dist = Math.sqrt((xDifference * xDifference)
				+ (yDifference * yDifference));
		return dist <= raggio;

		// boolean compresaX = false, compresaY = false;
		// if (x >= (centro.getX() - raggio) && x <= (centro.getX() + raggio)) {
		// compresaX = true;
		// }
		// if (y >= (centro.getY() - raggio) && y <= (centro.getY() + raggio)) {
		// compresaY = true;
		// }
		// return (compresaY && compresaX);
	}

	/**
	 * Returns a String that represents the point specified, with an
	 * approximation of 3 digits
	 */
	public String toString() {
		return "("
				+ new BigDecimal(getX()).setScale(3, BigDecimal.ROUND_UP)
						.doubleValue()
				+ ", "
				+ new BigDecimal(getY()).setScale(3, BigDecimal.ROUND_UP)
						.doubleValue() + ")";
	}
}
